package com.softaria.promosite.client;

import java.util.List;
import java.util.Map;

import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.softaria.gwt.common.client.dialog.YesNoDialog;
import com.softaria.gwt.common.client.dialog.YesNoDialog.YesNoListener;
import com.softaria.windows.logic.HelpWriter;
import com.softaria.windows.model.Size;
import com.softaria.windows.model.SizeCalculator;
import com.softaria.windows.model.View;
import com.softaria.windows.view.BadViewUrlException;
import com.softaria.windows.view.CloseConfirmation;
import com.softaria.windows.view.TypedViewFactory;
import com.softaria.windows.view.ViewCloseListener;
import com.softaria.windows.view.ViewCloseListenerRegistrator;
import com.softaria.windows.widgets.FrameResizeListener;
import com.softaria.windows.widgets.FrameResizeListenerRegistrator;

public class ListenersSampleTypedViewFactory implements TypedViewFactory {

  public View createView(Map<String, List<String>> params,
      ViewCloseListenerRegistrator closeListenerRegistrator, FrameResizeListenerRegistrator frlr,
      final HelpWriter helpWriter) throws BadViewUrlException {

    View view = new View();

    view.setSizeCalculator(new SizeCalculator() {

      public Size calculateSize(int width, int height) {
        return new Size() {

          public int getHeight() {
            return 200;
          }

          public int getWidth() {
            return 200;
          }

        };
      }

    });

    final Grid grid = new Grid(1, 1);

    grid.setStylePrimaryName("grids");

    final Label label = new Label();

    label.setStylePrimaryName("labels");

    grid.setWidget(0, 0, label);

    view.setContent(grid);
    view.setName("Resizing and closing");

    helpWriter.setHelpMessage("This window demonstrates work of listeners: "
        + "close listener and resize listener. When you try close the "
        + "window, window will ask you if you are sure, and only if yes,"
        + " it will allow system to close the window. It also shows its size");

    closeListenerRegistrator.setViewCloseListener(new ViewCloseListener() {

      public void beforeClose(final CloseConfirmation confirmation) {

        helpWriter
            .setHelpMessage("System got close event. Now it is asking window if it may close it or not."
                + "Window, in its turn, is asking you if it should be closed. Please answer");

        YesNoListener yesNoListener = new YesNoListener() {

          public void onNo() {
            helpWriter.setHelpMessage("Ok. Window restricts the system from closing it");
          }

          public void onYes() {
            confirmation.confirmClose();
          }
        };

        YesNoDialog question = new YesNoDialog("yes", "no", "Confirmation",
            "Do you really want to close this window?", yesNoListener);

        question.center();

      }

      public void onClose() {

      }

    });

    frlr.addResizeListener(new FrameResizeListener() {

      public void onResize(int height, int width) {

        grid.setSize(width + "px", height + "px");

        label
            .setText("Try resize or close window. Or even close whole perspective. Current size is "
                + height + "/" + width);

      }

    });

    return view;
  }
}
